/*给Vue实例添加原型属性，所有的Vue实例都拥有了$http属性*/
Vue.prototype.$http = axios;
//axios.defaults.baseURL = "http://api.pethome.com";//换成你自己后台访问根URL
axios.defaults.baseURL = "http://localhost:80";//换成你自己后台访问根URL
//这个参数，设置的是图片查看的全局服务器的地址
Vue.prototype.$fastBaseUrl = "http://47.95.117.210:8888";


/*axios前置拦截器， 只要你发送axios之前，都先执行此箭头函数*/
axios.interceptors.request.use(config => {
    //携带token
    let token = sessionStorage.getItem("token");
    if (token) {
        //添加请求头信息
        config.headers['token'] = token;
    }
    return config;
}, error => {
    Promise.reject(error);
});


//aixos后置拦截器，在响应目标函数之前，先执行此箭头函数
axios.interceptors.response.use(result => {
    let data = result.data;
    if (data.code === "2000") {
        //把token和登录用户删除掉
        localStorage.removeItem("token");
        //把登录用户删除吊
        localStorage.removeItem("userInfo");
        //跳转到登录界面
        location.href = "/login.html";
        return;
    }
    return result;
}, error => {
    Promise.reject(error);
});

/**
 * js动态获取?后面的参数，并且封装成一个json对象
 * @returns {Object}
 */
function getParam(){
    let myHref = location.href;
    console.log(myHref)//http://bugtracker.itsource.cn:8081/index.html?token=c5f690566c6f4ed38e437b26cca1d6fc&phone=17799990000

    var url=location.search;//?token=c5f690566c6f4ed38e437b26cca1d6fc&phone=17799990000
    console.log(url)
    var param = new Object();

    console.log(url.indexOf("?")) //0
    if(url.indexOf("?")!=-1){
        var str = url.substr(1)
        console.log(str) //token=c5f690566c6f4ed38e437b26cca1d6fc&phone=17799990000
        strs = str.split("&");
        console.log(strs) // Array [ "token=c5f690566c6f4ed38e437b26cca1d6fc", "phone=17799990000" ]

        for(var i=0;i<strs.length;i++){
            console.log(strs[i].split("=")) //[ "token", "c5f690566c6f4ed38e437b26cca1d6fc" ]
            let  key = strs[i].split("=")[0];

            let value = strs[i].split("=")[1]
            console.log(key,value) //token c5f690566c6f4ed38e437b26cca1d6fc

            //给这个对象赋值属性
            param[key]=unescape(value);
        }
    }
    console.log(param)//Object { token: "c5f690566c6f4ed38e437b26cca1d6fc", phone: "17799990000" }
    //param.token  param.phone
    return param;
}